/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CPayer.h
//
//		Author			:		Vindicated Games
//
//		Purpose			:		Encapsulate all functionality in the Player class
//
//		Dates			:		Written( Feb 15, 2011 ) by Vindicated Games		LastUpdate( March 3, 2011  ) by Jayson Wu
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CRock.h"


#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../Game States/CGamePlayState.h"
#include "../Managers/VIN_SoundManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../Core/MessageSystem.h"
#include "../Core/CGame.h"
#include "CMeleeEnemy.h"

CRock::CRock()
{
	m_nHealth = 500;
	//ImFree = true;
	SetHP(m_nHealth);
	SetType(OBJ_ROCK);
	//SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/Fat Tallnut.png"));
	SetGroundPt(90);
	//SetWidth(128);
	//SetHeight(128);

	fullLifeImage = MessageSystem::GetInstance()->GetStaticImageID(IMAGE_WALNUT);
	halfLifeImage = MessageSystem::GetInstance()->GetStaticImageID(IMAGE_WALNUT2);
	noLifeImage = MessageSystem::GetInstance()->GetStaticImageID(IMAGE_WALNUT3);

	SetImageID(fullLifeImage);
}

CRock::~CRock()
{
	//CSGD_TextureManager::GetInstance()->UnloadTexture(GetImageID());
}

CRock* CRock::GetInstance()
{
	static CRock instance;
	return &instance;
}

void CRock::Render()
{
	CSGD_TextureManager* pTM  = CSGD_TextureManager::GetInstance();
	CSGD_Direct3D*		pD3D = CSGD_Direct3D::GetInstance();

	pTM->Draw(GetImageID(),(int)GetPosX(),(int)GetPosY() - 100 );
	//pD3D->DrawRect(GetHealthRect(),0,255,0);
}

RECT CRock::GetHealthRect()
{
	SetRect(&RockRect, (int)GetPosX(), (int)GetPosY() - 100, (int)GetPosX() + GetWidth(), (int)GetPosY() + GetHeight() - 100);
	return RockRect;
}

bool CRock::Input()
{
	return true;
}

void CRock::Update(float fElapsedTime)
{
	SetWidth(CSGD_TextureManager::GetInstance()->GetTextureWidth(GetImageID()));
	SetHeight(CSGD_TextureManager::GetInstance()->GetTextureHeight(GetImageID()));

	if(GetHP() <= 0 )
	{
		MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this,2));
		CGamePlayState::GetInstance()->GetHand()->SetRockActive(false);
		//// and player is free to make new one
		//ImFree = false;
	}

	if(GetHP() <= (GetMaxHealth() * 0.5f))
	{
		SetImageID(halfLifeImage);

		if(GetHP() <= (GetMaxHealth() * 0.25f))
		{
			SetImageID(noLifeImage);
		}		
	}


	SetVelY(300);
	CBaseObject::Update( fElapsedTime );
}

bool CRock::CheckCollision( IBaseInterface* pBase )
{
	if(pBase->GetType() == OBJ_ENEMY )
	{
		CBaseEnemy* enemy = (CBaseEnemy*)pBase;

		RECT TempRect;

		if(IntersectRect(&TempRect, &GetHealthRect(), &enemy->GetHealthRect() ) )
		{
			SetHP(GetHP() - .5f);
			return true;		
		}
	}

	return false;
}
